package pe.edutec.app.layer.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import pe.edutec.app.domain.Empleado;
import pe.edutec.app.layer.dao.interfaces.EmpleadoDao;

/**
 *
 * @author Gustavo Coronel
 */
public class EmpleadoDaoImpl implements EmpleadoDao {

	@Override
	public Empleado leerPorId(String id) {
		Connection cn = null;
		Empleado emp = null;
		try {
			cn = AccesoDB.getConnection();
			String sql = "select IdEmpleado, Clave, ApeEmpleado, NomEmpleado,"
							+ "Cargo, DirEmpleado, TelEmpleado, EmailEmpleado "
							+ "from empleado where idempleado = ?";
			PreparedStatement pstm = cn.prepareStatement(sql);
			pstm.setString(1, id);
			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				emp = rowToBean(rs);
			}
			rs.close();
			pstm.close();
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage());
		} catch (Exception e) {
			throw new RuntimeException("Error al consultar tabla.");
		} finally {
			try {
				cn.close();
			} catch (Exception e) {
			}
		}
		return emp;
	}

	@Override
	public List<Empleado> leerPorApellido(String apellido) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void crear(Empleado empleado) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void modificar(Empleado empleado) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void eliminar(String id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	private Empleado rowToBean(ResultSet rs) throws SQLException {
		Empleado emp = new Empleado();
		emp.setId(rs.getString("idempleado"));
		emp.setClave(rs.getString("clave"));
		emp.setApellido(rs.getString("apeempleado"));
		emp.setNombre(rs.getString("nomempleado"));
		emp.setCargo(rs.getString("cargo"));
		emp.setDireccion(rs.getString("dirempleado"));
		emp.setTelefono(rs.getString("telempleado"));
		emp.setEmail(rs.getString("emailempleado"));
		return emp;
	}
}
